<template>
  <el-cascader
    size="mini"
    :options="treeData"
    :props="props"
    :value="value"
    separator=" | "
    @change="changeValue"
  />
</template>

<script>
import { getDepartment } from '@/api/department'
import { ListToTree } from '@/utils'

export default {
  name: 'SelectTree',
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      treeData: [],
      props: {
        label: 'name',
        value: 'id'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.treeData = ListToTree(await getDepartment(), 0)
    },
    changeValue(arr) {
      if (arr.length > 0) {
        // 传出自定义事件
        this.$emit('input', arr[arr.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>
